<?php
/**
* LinkedIn Habari Plugin
* This plugin allows the blog owner to show the LinkedIn Badge on the blog and add a share button to each post, to share your contents with the visitor linkedin connections
*
* @author Jose da Silva <jose@josedasilva.net>
* @since 2008-03-12
* @version 0.5
* 
* Example usage in PHP template:
* <code>
* <?php
* $theme->show_plugin_linkedin();
* ?>
* </code>
*/

class LinkedIn extends Plugin
{
	/**
	* Required plugin information
	* @return array The array of information
	*/
	function info()
	{
		return array(
			'name'		=>	'LinkedIn Plugin',
			'version'	=>	'0.5',
			'url'		=>	'http://www.josedasilva.net/downloads/habari/plugins/',
			'author'	=>	'Jose da Silva',
			'authorurl'	=>	'http://www.josedasilva.net',
			'licence'	=>	'Apache licence 2.0',
			'description'	=>	'LinkedIn Plugin'
		);
	}

	/**
	* Add actions to the plugin page for this plugin
	* @param array $actions An array of actions to apply to this plugin
	* @param string $plugin_id The string with the plugin id, generated by the system
	* @return array $actions Array of actions to atach to the specified $plugin_id
	*/
	public function  filter_plugin_config($actions,$plugin_id)
	{
		if( $plugin_id == $this->plugin_id()) {
			$actions[]	=	_t('Configure');
		}
		return $actions;
	}

	/**
	* Method that responds to the user selecting an action on the plugin page
	* @param string $plugin_id String containning the id of the plugin
	* @param string $action The action string suplied via the filter_plugin_config hook
	**/
	public function action_plugin_ui( $plugin_id, $action )
	{
		if ( $plugin_id == $this->plugin_id() ) {
			switch( $action ) {
				case _t('Configure'):
					$ui	=	new FormUI ( strtolower( get_class( $this ) ) );
					$ui->append('checkbox','showbadge','linkedin__showbadge',_t('Show badge on sidebar'));
					$ui->append('text','profilename','linkedin__profilename',_t('Profile:'));
					$ui->append('text','badgeurl','linkedin__badgeurl',_t('Badge Url:'));

					$ui->append('checkbox','useshare','linkedin__useshare',_t('Share Post With LinkedIn Connections:'));
					$ui->append('text','share_lbl','linkedin__share_lbl',_t('Share Label:'));
					$ui->append('submit', 'save', 'Save');

					$ui->out();
				break;
			}
		}
		
	}

	/**
	* Assigns output code to the template variables
	* @param Theme $theme The theme taht will display the template	
	*/
	function theme_show_plugin_linkedin()
	{
		$shareBadge 	= Options::get( 'linkedin__showbadge' );
		$pluginProfile	= Options::get( 'linkedin__profilename' );
		$badgeUrl	= Options::get( 'linkedin__badgeurl' );

		$code = '';
		if ($shareBadge)
		{
			$linkedin_badge= '<a href="http://www.linkedin.com/in/'.$pluginProfile.'" rel="external"><img src="'.$badgeUrl.'" border="0" alt="View '.$pluginProfile.' profile on LinkedIn"></a>';	
			$code = '<div class="linkedinplugin">'.$linkedin_badge.'</div>';
		}
		return $code;
	}

	public function filter_post_content_out( $content, $post )
	{

		$linkedinShare  = Options::get( 'linkedin__useshare' );
		$linkedinShareLbl = Options::get( 'linkedin__share_lbl' );

		$addShare = '';
		if( $linkedinShare )
		{
			$link  	= 	'http://www.linkedin.com/shareArticle?mini=true&url='.urlencode($post->permalink).'&title='.urlencode($post->title).'&summary='.urlencode(substr($content,0,200)).'&source='.urlencode($post->permalink);
			$addShare = '<div class="linkedin-share"><a href="'.$link.'" rel="external">'.$linkedinShareLbl.'</a></div>';
		}
		return $content.$addShare;
	}

	public function action_init() {
		Stack::add( 'template_stylesheet', array( Site::get_url('user') . '/plugins/linkedin/linkedin.css', 'screen,projector'), 'linkedin-css' );
		Stack::add( 'template_header_javascript', Site::get_url('scripts') . '/jquery.js', 'jquery' );
		Stack::add( 'template_header_javascript', Site::get_url('user') . '/plugins/linkedin/linkedin.js', 'linkedin-js' );
	}

	/**
	* Returns true if plugin config form values defined in action_plugin_ui whould be stored by habari
	* @return bool True if options should be stored
	*/
	public function updated_config($ui)
	{
		return true;
	}

}
?>
